package com.example.enginetestandroid;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.example.enginetestandroid.adapter.MainFragmentAdapter;
import com.example.enginetestandroid.fragment.HomeFragment;
import com.example.enginetestandroid.fragment.UserFragment;
import com.example.enginetestandroid.interfac.OnClickTabListener;
import com.example.enginetestandroid.manager.AppFlutterEngineManager;
import com.example.enginetestandroid.widget.CommonBottomBar;

import java.util.ArrayList;
import java.util.List;

import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class MainActivity extends AppCompatActivity {

    private CommonBottomBar bottomBar;
    private List<Fragment> fragments = new ArrayList<>();
    private HomeFragment homeFragment;
    private UserFragment userFragment;
    private MainFragmentAdapter mainFragmentAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initFragment();
        initView(savedInstanceState);
    }

    private void initFragment() {
        homeFragment = new HomeFragment();
        fragments.add(homeFragment);

        userFragment = new UserFragment();
        fragments.add(userFragment);

        mainFragmentAdapter = new MainFragmentAdapter(fragments, MainActivity.this, R.id.frame_centent, CommonBottomBar.HOME);
    }

    private void initView(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);

        bottomBar = findViewById(R.id.bar_bottom);
        bottomBar.setHome();
        bottomBar.setOnClickTabListener(new OnClickTabListener() {
            @Override
            public void onclick(int position) {
                switch (position) {
                    case CommonBottomBar.HOME:
                        mainFragmentAdapter.seletFragmentIndex(CommonBottomBar.HOME);
                        break;
                    case CommonBottomBar.ME:
                        mainFragmentAdapter.seletFragmentIndex(CommonBottomBar.ME);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * 展示隐藏TabBar
     */
    public void switchBottomView(boolean show) {
        if (show) {
            bottomBar.setVisibility(View.VISIBLE);
        } else {
            bottomBar.setVisibility(View.INVISIBLE);
        }
    }

}
